`
https://leetcode.cn/problems/search-a-2d-matrix/
`

/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function (matrix, target) {
  const m = matrix.length, n = matrix[0].length

  let left = 0, right = m * n - 1

  while (left <= right) {
    const mid = left + Math.floor((right - left) / 2)
    const midVal = get(matrix, mid)
    if (midVal > target) {
      right = mid - 1
    } else if (midVal < target) {
      left = mid + 1
    } else {
      return true
    }
  }

  return false
};

// 通过一维坐标访问二维数组中的元素
function get(matrix, index) {
  const n = matrix[0].length
  const i = Math.floor(index / n), j = index % n;
  return matrix[i][j];
}